package com.coderxq.blog.utils;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Author: lijingming
 * @Date: 2024/12/11
 * @Description:
 **/
@Service
@Data
public class ScheduledTaskControl {


    @Autowired
    private ThreadPoolTaskScheduler taskScheduler; //定时任务线程池

    private Map<String, ScheduledFuture> TimerTaskMap =  new HashMap<>(); //存储定时任务的Future对象

    private Map<String, ScheduledFuture<?>> DelayTaskMap = new HashMap<>(); //存储定时任务的Future对象

    /**
     *
     * @param startTime 定时任务每天开始时间
     * @param callback 回调函数
     * @param TaskName 定时任务名称
     */
    public void scheduleTimerTaskWithFixed(Date startTime, Callback callback,String TaskName) {
        Runnable task = () -> {
            callback.onCallback("参数1");
            // 执行SQL语句，例如删除用户
        };

        CronTrigger trigger = new CronTrigger("0 " + startTime.getMinutes() + " " + startTime.getHours() + " * * ?");
        ScheduledFuture scheduledFuture= taskScheduler.schedule(task, trigger);
        TimerTaskMap.put(TaskName, scheduledFuture);
    }

    /**
     * 取消指定名称的定时任务
     * @param taskName
     */
    public void cancelTimerTask(String taskName) {
        ScheduledFuture scheduledFuture = TimerTaskMap.get(taskName);
        if (scheduledFuture != null) {
            scheduledFuture.cancel(true);
            TimerTaskMap.remove(taskName);
        }
    }

    /**
     * 从当前时刻开始每隔10秒钟触发一次的定时任务
     * @param callback 回调函数
     * @param taskName 定时任务名称
     */
    public void scheduleTaskAtFixedRate(Callback callback, String taskName) {
        Runnable task = () -> {
            callback.onCallback("参数1");
            // 执行SQL语句，例如删除用户
        };

        long initialDelay = 0; // 初始延迟时间为0秒
        long period = 10; // 执行间隔为10秒

        ScheduledExecutorService executorService = taskScheduler.getScheduledExecutor();
        ScheduledFuture<?> scheduledFuture = executorService.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
        TimerTaskMap.put(taskName, scheduledFuture);
    }


    /**
     * 调度一个延时任务，3分钟后执行
     * @param callback 回调函数
     * @param taskName 定时任务名称
     */
    public void scheduleDelayedTask(Date startTime,Callback callback, String taskName) {
        Runnable task = () -> {
            try {
                callback.onCallback("参数1");
                // 在这里执行具体的业务逻辑
            } finally {
                cancelDelayTask(taskName); // 任务执行后取消任务
            }
        };

        Date countdownTime = new Date(startTime.getTime() + 3 * 60 * 1000); // 3分钟后执行
        ScheduledFuture<?> scheduledFuture = taskScheduler.schedule(task, countdownTime);
        DelayTaskMap.put(taskName, scheduledFuture);
    }

    /**
     * 取消指定名称的定时任务
     * @param taskName
     */
    public void cancelDelayTask(String taskName) {
        ScheduledFuture<?> scheduledFuture = DelayTaskMap.get(taskName);
        if (scheduledFuture != null) {
            scheduledFuture.cancel(true);
            DelayTaskMap.remove(taskName);
        }
    }
}
